package com.manage.adapter;

import java.util.ArrayList;

import com.manage.R;
import com.manage.model.OrderItem;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

/**
 * 订单可以增加、删除
 * @author liyoro
 *
 */
public class OrderAdapter extends BaseAdapter {

	public ArrayList<OrderItem> list = new ArrayList();
	private LayoutInflater layoutInflater;
	private int editOrder = View.GONE;

//	
	private boolean clearList;
	
	public OrderAdapter(Context paramContext) {
		this.layoutInflater = LayoutInflater.from(paramContext);
	}
	
	@Override
	public int getCount() {
		int i = this.list.size();
	    return i;
	}

	@Override
	public Object getItem(int position) {
		return this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		final int index = position;
		View view = convertView;
		
		if (view == null) {
			view = this.layoutInflater.inflate(R.layout.item_modify_order, null);
		}

	    final OrderItem localOrder = (OrderItem)this.list.get(position);

		final TextView textView1 = (TextView) view.findViewById(R.id.tv_name_order_item);
		textView1.setText(localOrder.getCaipin_name());
		
		final TextView textView2 = (TextView) view.findViewById(R.id.tv_count);
		textView2.setText(localOrder.getCaipin_count());

		final TextView textView3 = (TextView) view.findViewById(R.id.tv_name_order_price);
		textView3.setText("￥" + localOrder.getCaipin_price());
		
		final Button button1 = (Button) view.findViewById(R.id.bt_add);
		button1.setVisibility(this.editOrder);
		button1.setTag(position);
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int val1 = Integer.valueOf(localOrder.getCaipin_count()) + 1;
				if(val1 > 0) {
					localOrder.setCaipin_count(String.valueOf(val1));
					notifyDataSetChanged();
				}
			}
		});
		
		final Button button2 = (Button) view.findViewById(R.id.bt_minus);
		button2.setVisibility(this.editOrder);
		button2.setTag(position);
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int val2 = Integer.valueOf(localOrder.getCaipin_count()) - 1;
				if(val2 > 0) {
					localOrder.setCaipin_count(String.valueOf(val2));
					notifyDataSetChanged();
					
				}
			}
		});
		
		return view;
	}

	public void showEditButton(int paramInt) {
		this.editOrder = paramInt;
		notifyDataSetChanged();
	}

	public void setValue(ArrayList<OrderItem> paramArrayList) {
		if(getClear()) {
			this.list.clear();
		}
		
		if(paramArrayList != null) {
			this.list.addAll(paramArrayList);
		}
		
//		this.list.clear();
//		this.list = paramArrayList;
		notifyDataSetChanged();
	}
	
	/**
	 * 获取订单总价格
	 * @return
	 */
	public String getTotalPrice() {
		float totalPrice = 0.0f;
		
		for(int i=0; i< this.list.size(); i++) {
			OrderItem item = this.list.get(i);
			int count = Integer.valueOf(item.getCaipin_count());
			Log.i("count", ":"+count);
			float price = Float.valueOf(item.getCaipin_price());
			Log.i("price", ":"+price);
			totalPrice += count * price;
			Log.i("totalPrice", ":"+totalPrice);
		}
		return String.valueOf(totalPrice);
	}
	

	/**
	 * 设置是否清空list中数据，防止分页数据没了
	 * @param isClear
	 */
	public void setClear(boolean isClear) {
		this.clearList = isClear;
	}
	
	public boolean getClear() {
		return this.clearList==true?true:false;
	}
	
//	static class ViewHolder {
//		TextView localTextView1; // 菜单名
//		TextView localTextView2;
//		TextView localTextView3;
//		Button localButton1;
//		Button localButton2;
//	}
}
